class Entity extends GameObject {
	public type: number;
	public level: number;
	private AI: BehaviorTree;


	public hp: number;
	public maxhp: number;
	public atk: number;
	public def: number;
	public speed: number;
	/**技能列表 */
	private skillList: any

	/**延时行为 */
	private delayFuncs: any;

	public constructor(type, level) {
		super()
		this.type = type;
		this.level = level;
		this.skillList = {}
		this.delayFuncs = {}
		var prop = PropertyMaker.GenerateProperty(type, level, 1);
		this.setProperty(prop);
	}

	public setProperty(propData: any) {
		this.hp = propData.h;
		this.maxhp = propData.h;
		this.def = propData.d;
		this.atk = propData.a;
		this.speed = propData.s;
	}

	private body: ModelEffect;

	public setModel(modelName: string) {
		if (this.body == null) {
			this.body = new ModelEffect(modelName);
		}
		this.body.anchorOffsetX = 16;
		this.body.anchorOffsetY = 16;
		this.body.setModel(modelName);
	}

	public get x(): number {
		return this.body.x;
	}
	public set x(v: number) {
		this.body.x = v;
	}

	public get y(): number {
		return this.body.y;
	}
	public set y(v: number) {
		this.body.y = v;
	}

	public get rotation(): number {
		return this.body.rotation;
	}
	public set rotation(v: number) {
		this.body.rotation = v;
	}


	public setAI(aiName: string) {
		this.AI = AIManager.GetAI(aiName);
	}

	public OnTick(dt) {
		super.OnTick(dt);
		if (this.AI) {
			this.AI.think(this);
		}
		for (var k in this.delayFuncs) {
			this.delayFuncs[k].excute();
		}
	}
	///////////////////////////////////////////////////////////////
	/**施法 */
	public spell(skillId) {
		var skill = this.skillList[skillId] as Skill;
		if (skill.CanSpell()) {
			this.showSkillWarn(skill.config.type, skill.config.range, skill.config.pre);
			skill.OnSpell();

			//延时攻击
			this.addSkillDelayAttackTasks(skillId, skill.config.pre);
		}
	}
	/**触发伤害 */
	public attack(skillId) {
		var skill = this.skillList[skillId] as Skill;
		var targets = skill.pickTargets(this.x, this.y, this.rotation);
		for (var k in targets) {
			this.attackTarget(targets[k], skill);
		}
	}
	//延时攻击
	public addSkillDelayAttackTasks(skillId, delay) {
		if (this.delayFuncs["satk" + skillId]) {
			this.delayFuncs["satk" + skillId].reset(delay);
		} else {
			this.delayFuncs["satk" + skillId] = new DelayFuncs(delay, () => {
				this.attack(skillId);
			});
		}
	}

	/**攻击目标 */
	public attackTarget(target: GameObject, skill: Skill) {

	}

	public learnSkill(skillId) {
		this.skillList[skillId] = SkillFactory.CreateSkill(skillId);
	}

	public showSkillWarn(type: number, range: number, time: number) {
		switch (type) {
			case 1:
				SkillWarn.CreateWarn(type, range, this.x, this.y, this.rotation, time);
				break;
			default:
				break;
		}
	}
}